package com.css.fxfzfxqh.modules.util.repository.impl;

import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.YSXMainTable;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.util.YSXEntity;
import com.css.fxfzfxqh.modules.util.repository.YSXRepository;
import com.css.fxfzfxqh.modules.ysx.entity.FxqhYSX;
import com.css.fxfzfxqh.modules.ysx.param.YSXParam;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/07/21
 * @Description: 易损性临时
 */
@Repository
public class YSXRepositoryImpl implements YSXRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public List<YSXEntity> getYSX(String pid, String cid, String id) {
        String sql = "select * from ysx where province_id = '" + pid + "' and city_id= '" + cid + "'";
        if (StringUtils.isNotBlank(id)) {
            sql += " and county_id='" + id + "'";
        }
        List<YSXEntity> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(YSXEntity.class));
        return list;
    }

    @Override
    public List<YSXMainTable> getYSX1(String type1, String type2, String damageDegree, String intensity, String pid,
                                      String cid, String id, String taskId) {
        StringBuilder sql = new StringBuilder("SELECT * FROM " +
                "(select * from fxqh_jzwysx_copy " +
                " where task_id='" + taskId + "' and province='" + pid + "' and city='" + cid + "' and county='" + id + "' " +
                " and structure_type1='" + type1 + "' ");
        if (!PlatformObjectUtils.isEmpty(type2)) {
            sql.append(" and structure_type2 = '"+type2+"' ");
        }
        sql.append(" ) a  left join fxqh_jzwysx_sub_copy b on a.id = b.pid where b.intensity='"+intensity+"' and b.damage='"+damageDegree+"'");
        List<YSXMainTable> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(YSXMainTable.class));
        return list;
    }
    @Override
    public List<YSXMainTable> getYSX2(String taskId, String pName, String cName, String name) {
        StringBuilder sql = new StringBuilder("SELECT * FROM " +
                "(select * from fxqh_jzwysx_copy " +
                " where task_id='" + taskId + "' and province='" + pName + "' and city='" + cName + "' and county='" + name + "' ");

        sql.append(" ) a  left join fxqh_jzwysx_sub_copy b on a.id = b.pid ");
        List<YSXMainTable> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(YSXMainTable.class));
        return list;
    }


    //易损性分页查询,正式版
    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, YSXParam param) {
        StringBuilder sql = new StringBuilder("select * from fxqh_ysx ");
        StringBuilder whereSql = new StringBuilder(" where 1 = 1");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        //省市县
        if (!PlatformObjectUtils.isEmpty(param.getProvinceId())) {
            whereSql.append(" and province_id = '" + param.getProvinceId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getCityId())) {
            whereSql.append(" and city_id = '" + param.getCityId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getCountyId())) {
            whereSql.append(" and county_id = '" + param.getCountyId() + "'");
        }
        //结构类型
        if (!PlatformObjectUtils.isEmpty(param.getStructureType())) {
            whereSql.append(" and structure_type = '" + param.getStructureType() + "'");
        }
        //设防情况
        if (!PlatformObjectUtils.isEmpty(param.getFortify())) {
            whereSql.append(" and fortify = '" + param.getFortify() + "'");
        }
        //年代
        if (!PlatformObjectUtils.isEmpty(param.getYear())) {
            whereSql.append(" and year = '" + param.getYear() + "'");
        }
        //房屋用途
        if (!PlatformObjectUtils.isEmpty(param.getHousingUse())) {
            whereSql.append(" and housing_use = '" + param.getHousingUse() + "'");
        }
        //层数
        if (!PlatformObjectUtils.isEmpty(param.getNumberOfFloors())) {
            whereSql.append(" and number_of_floors = '" + param.getNumberOfFloors() + "'");
        }
        sql.append(whereSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(FxqhYSX.class)).size();
        List<FxqhYSX> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(FxqhYSX.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public List<YSXEntity> getYSXByType(String pid, String cName, String id, String type) {
        String sql = "select * from ysx where province_id = '" + pid + "' and city_id= '" + cName + "' and structure_type='" + type + "'";
        if (StringUtils.isNotBlank(id)) {
            sql += " and county_id='" + id + "'";
        }
        List<YSXEntity> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(YSXEntity.class));
        return list;
    }

    @Override
    public void saveYsx(YSXEntity ysxEntity, FxqhYSX fxqhYSX) {
        String sql = "insert into ysx (id, province_id, city_id, county_id, structure_type, fortify, damage_degree, intensity6, intensity7, intensity8, intensity9, intensity10) values (?,?,?,?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql, ysxEntity.getId(), ysxEntity.getProvince_id(), ysxEntity.getCity_id(), ysxEntity.getCounty_id(), ysxEntity.getStructure_type(), ysxEntity.getFortify(), ysxEntity.getDamage_degree(), ysxEntity.getIntensity6(), ysxEntity.getIntensity7(), ysxEntity.getIntensity8(), ysxEntity.getIntensity9(), ysxEntity.getIntensity10());

        String fxqhSql = "insert into fxqh_ysx (id, province_id, city_id, county_id, structure_type, fortify, damage_degree, intensity6, intensity7, intensity8, intensity9, intensity10, year, housing_use, number_of_floors) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(fxqhSql, ysxEntity.getId(), ysxEntity.getProvince_id(), ysxEntity.getCity_id(), ysxEntity.getCounty_id(), ysxEntity.getStructure_type(), ysxEntity.getFortify(), ysxEntity.getDamage_degree(), ysxEntity.getIntensity6(), ysxEntity.getIntensity7(), ysxEntity.getIntensity8(), ysxEntity.getIntensity9(), ysxEntity.getIntensity10(), null, null, null);

    }



}
